/*
 * @Author: your name
 * @Date: 2021-01-28 08:32:04
 * @LastEditTime: 2021-01-28 10:38:18
 * @FilePath: \flutter_study\lib\pages\router_demo.dart
 */
import 'package:flutter/material.dart';
import 'package:timor_flutter/mock/posts.dart';

class RouterMain extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: RouterDemo(),
    );
  }
}

// 路由演示代码
class RouterDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            FlatButton(
              child: Text("Home"),
              onPressed: null,
            ),
            FlatButton(
              child: Text("About"),
              onPressed: () {
                // Navigator.of(context)
                //     .push(MaterialPageRoute(
                //         builder: (BuildContext context) =>
                //             PageDemo(title: "About")))
                //     .then((value) => print(value));
                // Navigator.of(context).pushNamed('/pagedemo');
                Navigator.of(context).pushNamedAndRemoveUntil(
                    '/pagedemo', (Route route) => false); // 跳转路由并关闭所有其他页面
              },
            )
          ],
        ),
      ),
    );
  }
}

// 路由跳转页面
class PageDemo extends StatelessWidget {
  final String title;
  PageDemo({@required this.title});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.arrow_back),
        onPressed: () {
          Navigator.pop(context, 123);
          //   Navigator.of(context).pushReplacementNamed('/'); //替换路由
          //   Navigator.pushReplacementNamed(context, '/router');
          //   Navigator.pushReplacement(
          //       context,
          //       new MaterialPageRoute(
          //           builder: (BuildContext context) => RouterMain()));
        },
      ),
    );
  }
}

// 路由跳转页面
class PageDetailDemo extends StatelessWidget {
  final Post post;
  PageDetailDemo({@required this.post});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(post.title),
      ),
      body: Column(
        children: <Widget>[
          Image.network(post.imageUrl, fit: BoxFit.cover),
          Container(
            padding: EdgeInsets.all(16.0),
            child: Column(
              //   mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  post.title,
                  style: TextStyle(
                      fontSize: 16.0,
                      fontWeight: FontWeight.bold,
                      color: Colors.grey[800]),
                ),
                Text(
                  post.author,
                  style: TextStyle(
                      fontSize: 14.0,
                      fontWeight: FontWeight.bold,
                      color: Colors.grey[600]),
                ),
                SizedBox(height: 16.0),
                Text(
                  post.description,
                  style: TextStyle(
                      fontSize: 13.0,
                      fontWeight: FontWeight.bold,
                      color: Colors.grey[600]),
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}
